跳到主要内容

go-micro v3 添加链路追踪

jaeger 原理

引用:

docker-compose安装jaeger

version: '3'
services:
# 注册中心集群版本设置
consul1:
image: cap1573/consul
container_name: node1
# 以 server 模式启动
command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
consul2:
image: cap1573/consul
container_name: node2
# 以 server 模式启动
command: agent -server -retry-join=node1 -bootstrap-expect=3 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
depends_on:
# 依赖consul1启动好之后
- consul1
consul3:
image: cap1573/consul
container_name: node3
# 以 server 模式启动
command: agent -server -retry-join=node1 -bootstrap-expect=3 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
depends_on:
# 依赖consul1启动好之后
- consul1
# 添加对外暴露的节点 启动控制面板
consul4:
image: consul
container_name: node4
command: agent -retry-join=node1 -node=node4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui
ports:
- 8500:8500
depends_on:
- consul2
- consul3
# 添加数据库
paas-mysql:
image: cap1573/mysql:5.6
environment:
MYSQL_ROOT_PASSWORD: 123456
container_name: paas-mysql
# 我们这里换个端口,本地已经有3306了
ports:
- "3308:3306"
# 需要将重要的数据挂盘
volumes:
- ./mysql:/var/lib/mysql
# 添加jaeger 服务
jaeger:
image: cap1573/jaeger
ports:
- "6831:6831/udp"
- "16686:16686"

添加好jaeger配置后,需要重启一下

docker-compose up

启动完成之后可以到浏览器去访问:http://127.0.0.1:16686/

image-20221106113229463

common统一模块添加jaeger

jaeger.go

package common

import (
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"io"
"time"
)

// NewTracer 创建jaeger链路追踪实例
func NewTracer(serviceName string, addr string) (opentracing.Tracer, io.Closer, error) {
cfg := &jaegercfg.Configuration{
ServiceName: serviceName,
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
BufferFlushInterval: 1 * time.Second,
LocalAgentHostPort: addr,
},
}
tracer, closer, err := cfg.NewTracer()
return tracer, closer, err
}

base基础服务代码添加jaeger

go get github.com/opentracing/opentracing-go
go get github.com/asim/go-micro/plugins/wrapper/trace/opentracing/v3

还得拉取一下上面更新后的common模块,不然无法使用common模块中的NewTracer方法。

main.go

package main

import (
"fmt"
"github.com/wujie/base/handler"
base "github.com/wujie/base/proto/base"
"github.com/wujie/common"
"github.com/asim/go-micro/v3/registry"
"github.com/jinzhu/gorm"
"github.com/opentracing/opentracing-go"
//"github.com/afex/hystrix-go/hystrix"
"github.com/asim/go-micro/plugins/registry/consul/v3"
opentracing2 "github.com/asim/go-micro/plugins/wrapper/trace/opentracing/v3"
"github.com/asim/go-micro/v3"
_ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
//需要本地启动,mysql,consul中间件服务
//1.注册中心
//consul := consul.NewRegistry(func(options *registry.Options) {
// options.Addrs = []string{
// consulHost + ":" + strconv.FormatInt(consulPort, 10),
// }
//})
consul := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"localhost:8500",
}
})
//2.配置中心,存放经常变动的变量
consulConfig, err := common.GetConsulConfig("localhost", consulPort, "/micro/config")
if err != nil {
//common.Error(err)
fmt.Println(err)
}
fmt.Println(consulConfig)
//3.使用配置中心连接 mysql
// 这里的path是相对于 /micro/config 目录路径
mysqlInfo := common.GetMysqlFromConsul(consulConfig, "mysql")
//初始化数据库
fmt.Println(mysqlInfo)
db, err := gorm.Open("mysql", mysqlInfo.User+":"+mysqlInfo.Pwd+"@("+mysqlInfo.Host+":3308)/"+mysqlInfo.Database+"?charset=utf8&parseTime=True&loc=Local")
if err != nil {
//命令行输出下,方便查看错误
fmt.Println(err)
common.Fatal(err)
fmt.Println("mysql连接失败")
}
fmt.Println("mysql连接成功")
defer db.Close()
//禁止复表
db.SingularTable(true)

//4.添加链路追踪
//t, io, err := common.NewTracer("go.micro.service.base", tracerHost+":"+strconv.Itoa(tracerPort))
t, io, err := common.NewTracer("base", "localhost:6831")
if err != nil {
//common.Error(err)
fmt.Println(err)
}
defer io.Close()
// 设置全局变量
opentracing.SetGlobalTracer(t)

// 添加服务
service := micro.NewService(
micro.Name("base-wj"),
micro.Version("latest"),
// 添加注册中心
micro.Registry(consul),
// 添加链路追踪
micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
)

service.Init()

base.RegisterBaseHandler(service.Server(), new(handler.BaseHandler))

// 启动服务
if err := service.Run(); err != nil {
//输出启动失败信息
common.Fatal(err)
}
}

再次进行运行测试即可。

# 运行 base 服务
go run main.go

config
&{127.0.0.1 root 123456 paas }
mysql连接成功
2022-11-06 11:44:44 file=v3@v3.7.0/service.go:206 level=info Starting [service] base-wj
2022-11-06 11:44:44 file=server/rpc_server.go:820 level=info Transport [http] Listening on [::]:57588
2022-11-06 11:44:44 file=server/rpc_server.go:840 level=info Broker [http] Connected to 127.0.0.1:57589
2022-11-06 11:44:44 file=server/rpc_server.go:654 level=info Registry [consul] Registering node: base-wj-f63478c8-3e87-45d9-92fb-0e6c1dac2959